package com.chookss.home.search.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chookss.R;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.search.entity.SearchAllEntity;
import com.chookss.tiku.SearchSubjectDetailActivity;
import com.chookss.tiku.entity.Subject;
import com.chookss.tools.EntityUtils;
import com.chookss.tools.Utils;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class SearchTimuAdapter extends BaseQuickAdapter<SearchAllEntity.SubjectBean.ListBean, BaseViewHolderEx> {
    private Context context;
    private int width, height;
    private String key;

    public SearchTimuAdapter(int layoutResId, Context context, @Nullable List<SearchAllEntity.SubjectBean.ListBean> data) {
        super(layoutResId, data);
        this.context = context;

    }

    @Override
    protected void convert(BaseViewHolderEx helper, SearchAllEntity.SubjectBean.ListBean item) {
        TextView tvTiltle = helper.getView(R.id.tvSearchTimuTitle);
        TextView tvSearchType = helper.getView(R.id.tvSearchTimuType);
        TextView tvSearchProvider = helper.getView(R.id.tvSearchTimuProvider);
        TextView tvSearchKeywords = helper.getView(R.id.tvSearchTimuKeywords);
        tvTiltle.setText(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getSubjectContent(), key));
        tvSearchType.setText(item.getSubjectTypeName());
        tvSearchProvider.setText("提供者：");
        tvSearchProvider.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getProvideEmployeeName(), key));
        if (!Utils.isNull(item.getKeyWord())) {
            if (item.getKeyWord().contains(key)) {
                tvSearchKeywords.setVisibility(View.VISIBLE);
                tvSearchKeywords.setText("关键词：");
                tvSearchKeywords.append(Utils.matcherSearchTitle(context.getResources().getColor(R.color.main_color), item.getKeyWord(), key));
            } else {
                tvSearchKeywords.setVisibility(View.GONE);
            }
        } else {
            tvSearchKeywords.setVisibility(View.GONE);
        }
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SearchSubjectDetailActivity.class);
                Subject subject = EntityUtils.modelA2B(item);
                intent.putExtra("subject", subject);
                context.startActivity(intent);
            }
        });
    }

    public void setKey(String key) {
        this.key = key;
    }
}
